package com.lgkj.ui;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lgkj.R;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.TitleBuilder;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/11/20 0020.
 */

public class CardActivity extends BaseActivity {
    @BindView(R.id.tv_name)
    EditText etName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_identity)
    TextView tvIdentity;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.et_city)
    TextView etCity;
    @BindView(R.id.et_shop)
    EditText etShop;
    @BindView(R.id.et_shop_adress)
    EditText etShopAdress;
    @BindView(R.id.ll_city)
    LinearLayout llCity;
    @BindView(R.id.ll_shop)
    LinearLayout llShop;
    @BindView(R.id.ll_dizhi)
    LinearLayout llDizhi;
    private int type = -1;
    private String imageId = "";
    private String shenfen = "创客";

    @Override
    public int getLayoutId() {
        return R.layout.activity_card_1;
    }

    @Override
    public void initView() {
        new TitleBuilder(CardActivity.this).setTitleText("身份认证").setLeftIcoListening(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.ll_card, R.id.iv_save, R.id.ll_city, R.id.rb_item1, R.id.rb_item2, R.id.rb_item3})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_card:
//                chooiceSex();
                break;
            case R.id.iv_save:
                save();
                break;
            case R.id.ll_city:
                startsActivity(new Intent(CardActivity.this, ChoiceCity.class));
                break;
            case R.id.rb_item1:
                type = 2;
                getTexts();
                break;
            case R.id.rb_item2:
                type = 1;
                getTexts();
                break;
            case R.id.rb_item3:
                type = 0;
                getTexts();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        etCity.setText(ChoiceCity.cityText);
    }

    private String pathUrl = "";

    private void getTexts() {
        llCity.setVisibility(View.INVISIBLE);
        llDizhi.setVisibility(View.INVISIBLE);
        llShop.setVisibility(View.INVISIBLE);
        //"总代身份", "合伙人身份", "创客身份"
        switch (type) {
            case 0:
                pathUrl = CommonConstant.regAgent;
                shenfen = "城市代理身份";
                llCity.setVisibility(View.VISIBLE);
                break;
            case 1:
                shenfen = "合伙人身份";
                pathUrl = CommonConstant.regPartner;
                llCity.setVisibility(View.VISIBLE);
                llDizhi.setVisibility(View.VISIBLE);
                llShop.setVisibility(View.VISIBLE);
                break;
            case 2:
                pathUrl = CommonConstant.regEntrepreneur;
                shenfen = "创客身份";
                break;
            default:
                break;
        }
        tvTips.setText("您当前选择的身份是：" + shenfen);
        Log.i(TAG, "getTexts: " + type);
    }

    private void save() {
        Log.i(TAG, "save: " + etName.getText().toString());
        String url = "";
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(CardActivity.this));
        map.put("name", etName.getText().toString());
        map.put("phone", etPhone.getText().toString());
        map.put("city_id", ChoiceCity.cityId);
        map.put("province_id", ChoiceCity.province_id);
        map.put("barbershop_name", etShop.getText().toString());
        map.put("barbershop_address", etShopAdress.getText().toString());
        HttpUtils.postHttp(map, pathUrl, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("code") == 0) {
                    toastLong("身份申请认证成功，请耐心等待", 0);
                    finish();
                } else {
                    toastLong(jsonObject.optString("msg"), 0);
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }


}
